package com.gome.axwl.aoc.facade.impl;

import com.gome.axwl.aoc.common.util.RopRequestUtils;
import com.gome.axwl.aoc.facade.SmsSendFacade;
import com.gome.axwl.aoc.facade.UserFacade;
import com.gome.axwl.aoc.service.UserService;
import com.gome.commons.core.ErrorCode;
import com.gome.commons.core.ResultData;
import com.gome.commons.json.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : John Li
 * @Description: 用户
 * @Date: 2018/4/25 13:59
 */
@Service
public class SmsSendFacadeImpl implements SmsSendFacade {

    private final static Logger log = LoggerFactory.getLogger(SmsSendFacadeImpl.class);

    @Autowired
    private UserService userService;

    @Value("${msgPush.api}")
    private String sendSmsUrl;

    @Value("${msgPush.v}")
    private String version;

    @Value("${msgPush.appKey}")
    private String appKey;

    @Value("${msgPush.method}")
    private String method;

    @Value("${msgPush.messageFormat}")
    private String messageFormat;


    /**
     * 通用短信接口
     *
     * @param ropVersionInfo
     * @param params
     * @param serverUrl
     * @return
     */
    @Override
    public ResultData sendSms(Map<String, String> ropVersionInfo, String params, String serverUrl) {
        //调用接口
        Long current = System.currentTimeMillis();
        String result = RopRequestUtils.requestWithSign(ropVersionInfo, params, serverUrl);
        if (StringUtils.isBlank(result)) {
            return ResultData.newResultData(ErrorCode.FAILOR, "发送短信失败:" + params);
        }
        try {
            ResultData res = (ResultData) JsonUtil.toObject(result, ResultData.class);
            if (!ResultData.isSuccess(res)) {
                return ResultData.newResultData(ErrorCode.FAILOR, "发送短信失败:" + res.getMessage());
            } else {
                return ResultData.newResultData(ErrorCode.SUCCESS, ErrorCode.SUCCESS_MSG);
            }
        } catch (Exception e) {
            log.error("转换短信发送结果失败:{}", e);
            return ResultData.newResultData(ErrorCode.FAILOR, "查询用户数据失败:" + e.getMessage());
        } finally {
            log.info("-call sendSmsByThirdServer:{}", System.currentTimeMillis() - current);
        }
    }

    /**
     * 安迅物流专用短信接口
     *
     * @param phoneNos
     * @param text
     * @return
     */
    @Override
    public ResultData sendSmsWithAxwlConfig(String phoneNos, String text) {
        Long current = System.currentTimeMillis();
        Map<String, String> ropVersionInfo = buildRopVersionInfo();
        String params = "{\"phoneNo\": \"" + phoneNos + "\",\"content\": \"" + text + "\"}";
        ropVersionInfo.put("params", params);
        log.info("调用接口:{}, 参数:ropVersionInfo:{}", sendSmsUrl, ropVersionInfo);
        String result = RopRequestUtils.requestWithNoSign(ropVersionInfo, sendSmsUrl);
        if (StringUtils.isBlank(result)) {
            return ResultData.newResultData(ErrorCode.FAILOR, "发送安迅物流业务短信失败:" + params);
        }
        try {
            ResultData res = (ResultData) JsonUtil.toObject(result, ResultData.class);
            if (!ResultData.isSuccess(res)) {
                return ResultData.newResultData(ErrorCode.FAILOR, "发送安迅物流业务短信失败:" + res.getMessage());
            } else {
                return ResultData.newResultData(ErrorCode.SUCCESS, ErrorCode.SUCCESS_MSG);
            }
        } catch (Exception e) {
            log.error("转换安迅物流业务短信发送结果失败:{}", e);
            return ResultData.newResultData(ErrorCode.FAILOR, "安迅物流业务短信发送结果转换异常:" + e.getMessage());
        } finally {
            log.info("-call sendSmsByThirdServer:{}", System.currentTimeMillis() - current);
        }
    }

    private Map<String, String> buildRopVersionInfo() {
        Map<String, String> ropVersionInfo = new HashMap<String, String>();
        // <--指定方法名称
        ropVersionInfo.put("method", method);
        // <--指定消息应答格式json
        ropVersionInfo.put("messageFormat", messageFormat);
        // 版本号
        ropVersionInfo.put("v", version);
        // appKey
        ropVersionInfo.put("appKey", appKey);

        log.info("request sms url with Rop config:{}", ropVersionInfo);
        return ropVersionInfo;
    }
}
